package com.happy.cat.project.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.happy.cat.common.utils.SecurityUtils;
import com.happy.cat.common.utils.StringUtils;
import com.happy.cat.common.utils.poi.ExcelUtil;
import com.happy.cat.framework.web.controller.BaseController;
import com.happy.cat.framework.web.domain.AjaxResult;
import com.happy.cat.framework.web.page.TableDataInfo;
import com.happy.cat.framework.aspectj.lang.annotation.Log;
import com.happy.cat.framework.aspectj.lang.enums.BusinessType;
import com.happy.cat.project.system.domain.CatExchangeRate;
import com.happy.cat.project.system.service.ICatExchangeRateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 汇率设置Controller
 *
 * @author verne
 * @date 2020-07-18
 */
@AllArgsConstructor
@RestController
@RequestMapping("/system/exchangeRate" )
@Api("汇率设置 API")
public class CatExchangeRateController extends BaseController {

    private final ICatExchangeRateService iCatExchangeRateService;

    /**
     * 查询汇率设置列表
     */
    @ApiOperation(value = "查询汇率设置列表",notes = "查询汇率设置列表",response = TableDataInfo.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:list')")
    @GetMapping("/list")
    public TableDataInfo list(CatExchangeRate catExchangeRate)
    {
        startPage();
        LambdaQueryWrapper<CatExchangeRate> lqw = new LambdaQueryWrapper<CatExchangeRate>();
        if (StringUtils.isNotBlank(catExchangeRate.getName())){
            lqw.like(CatExchangeRate::getName ,catExchangeRate.getName());
        }
        if (StringUtils.isNotBlank(catExchangeRate.getRateKey())){
            lqw.eq(CatExchangeRate::getRateKey ,catExchangeRate.getRateKey());
        }
        if (StringUtils.isNotBlank(catExchangeRate.getOneType())){
            lqw.eq(CatExchangeRate::getOneType ,catExchangeRate.getOneType());
        }
        if (catExchangeRate.getOneNumber() != null){
            lqw.eq(CatExchangeRate::getOneNumber ,catExchangeRate.getOneNumber());
        }
        if (StringUtils.isNotBlank(catExchangeRate.getTwoType())){
            lqw.eq(CatExchangeRate::getTwoType ,catExchangeRate.getTwoType());
        }
        if (catExchangeRate.getTwoNumber() != null){
            lqw.eq(CatExchangeRate::getTwoNumber ,catExchangeRate.getTwoNumber());
        }
        if (catExchangeRate.getServiceCharge() != null){
            lqw.eq(CatExchangeRate::getServiceCharge ,catExchangeRate.getServiceCharge());
        }
        if (catExchangeRate.getCreateDate() != null){
            lqw.eq(CatExchangeRate::getCreateDate ,catExchangeRate.getCreateDate());
        }
        if (catExchangeRate.getStatus() != null){
            lqw.eq(CatExchangeRate::getStatus ,catExchangeRate.getStatus());
        }
        List<CatExchangeRate> list = iCatExchangeRateService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出汇率设置列表
     */
    @ApiOperation(value = "导出汇率设置列表",notes = "导出汇率设置列表",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:export')" )
    @Log(title = "汇率设置" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(CatExchangeRate catExchangeRate) {
        LambdaQueryWrapper<CatExchangeRate> lqw = new LambdaQueryWrapper<CatExchangeRate>(catExchangeRate);
        List<CatExchangeRate> list = iCatExchangeRateService.list(lqw);
        ExcelUtil<CatExchangeRate> util = new ExcelUtil<CatExchangeRate>(CatExchangeRate. class);
        return util.exportExcel(list, "exchangeRate" );
    }

    /**
     * 获取汇率设置详细信息
     */
    @ApiOperation(value = "获取汇率设置详细信息",notes = "获取汇率设置详细信息",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iCatExchangeRateService.getById(id));
    }

    /**
     * 新增汇率设置
     */
    @ApiOperation(value = "新增汇率设置",notes = "新增汇率设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:add')" )
    @Log(title = "汇率设置" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CatExchangeRate catExchangeRate) {
        catExchangeRate.setCreateDate(new Date());
        catExchangeRate.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatExchangeRateService.save(catExchangeRate) ? 1 : 0);
    }

    /**
     * 修改汇率设置
     */
    @ApiOperation(value = "修改汇率设置",notes = "修改汇率设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:edit')" )
    @Log(title = "汇率设置" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CatExchangeRate catExchangeRate) {
        catExchangeRate.setCreateDate(new Date());
        catExchangeRate.setCreateBy(SecurityUtils.getUsername());
        return toAjax(iCatExchangeRateService.updateById(catExchangeRate) ? 1 : 0);
    }

    /**
     * 删除汇率设置
     */
    @ApiOperation(value = "删除汇率设置",notes = "删除汇率设置",response = AjaxResult.class)
    @PreAuthorize("@ss.hasPermi('system:exchangeRate:remove')" )
    @Log(title = "汇率设置" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iCatExchangeRateService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
